package dry.example.abstracts;

import dry.example.zhujie.AddLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * @author zc
 */
public abstract class AbsAddLogInterceptor {

    /**
     * 切点
     */
    public abstract void execute();

    @Around("execute()")
    public Object goAddLog(ProceedingJoinPoint pjp) throws Throwable {
        run01(pjp);
        return pjp.proceed();
    }

    public void run01(ProceedingJoinPoint pjp) throws NoSuchMethodException {
        Class<?> theClass = pjp.getTarget().getClass();
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        Method method = theClass.getDeclaredMethod(ms.getName(), ms.getParameterTypes());
        AddLog addLog = method.getAnnotation(AddLog.class);
        if (addLog == null) {
            return;
        }
        System.out.println(addLog.value());
    }

}
